package com.push.spider.spider;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.push.common.config.TransConfig;
import com.push.common.manager.BeanManager;
import com.push.common.utils.DateUtils;
import com.push.common.utils.EncryptUtil;
import com.push.common.utils.MD5;
import com.push.common.utils.StringUtils;
import com.push.common.utils.httpclient.HttpClientResult;
import com.push.common.utils.httpclient.HttpClientUtil;
import com.push.spider.utils.UrlUtils;
import com.push.web.domain.BizArticle;
import com.push.web.domain.BizLabelSecond;
import com.push.web.domain.BizSource;
import com.push.web.service.IBizArticleAppendixService;
import com.push.web.service.IBizArticleService;
import com.push.web.service.IBizLabelSecondService;
import com.push.web.service.IBizSourceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JplNasaNewsSpider {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private ArrayList<String> hasDownUrls = new ArrayList<>();

    private BizSource source;

    private List<BizLabelSecond> labelSeconds = new ArrayList<>();

    public int size = 0;

    @Autowired
    IBizArticleService articleService = BeanManager.getBean(IBizArticleService.class);

    @Autowired
    IBizSourceService sourceService = BeanManager.getBean(IBizSourceService.class);

    @Autowired
    IBizLabelSecondService labelSecondService = BeanManager.getBean(IBizLabelSecondService.class);

    @Autowired
    IBizArticleAppendixService articleAppendixService = BeanManager.getBean(IBizArticleAppendixService.class);

    @Autowired
    private TransConfig transConfig = BeanManager.getBean(TransConfig.class);

    public JplNasaNewsSpider(String sourceId){
        this.source = sourceService.selectBizSourceBySourceId(Long.valueOf(sourceId));
        this.labelSeconds = labelSecondService.selectBizLabelSecondListByParentIds(this.source.getLabelIds());
    }

    public void run() throws InterruptedException {
        HashMap params = new HashMap();
        params.put("query", "");
        params.put("topics[]", "Earth");
        params.put("page", "1");
        params.put("sort", "");
        params.put("size", "12");
        params.put("content_type", "news.News");
        HttpClientResult clientResult = HttpClientUtil.doGet(source.getSpiderUrl(), params,true);
        JSONObject resultObj = JSONObject.parseObject(clientResult.getContent());
        int total = resultObj.getJSONObject("hits").getJSONObject("total").getInteger("value");
        System.out.println("总页数："+(total/12+1));
        int totalPages = total/12+1;
        for (int page = 1; page < totalPages; page++){
            HashMap paramsDetail = new HashMap();
            paramsDetail.put("query", "");
            paramsDetail.put("topics[]", "Earth");
            paramsDetail.put("page", String.valueOf(page));
            paramsDetail.put("sort", "");
            paramsDetail.put("size", "12");
            paramsDetail.put("content_type", "news.News");
            clientResult = HttpClientUtil.doGet(source.getSpiderUrl(), paramsDetail,true);
            resultObj = JSONObject.parseObject(clientResult.getContent());
            JSONArray jsonArray = resultObj.getJSONObject("hits").getJSONArray("hits");
            for (int i = 0; i < jsonArray.size(); i++){
                JSONObject articleObj =jsonArray.getJSONObject(i);
                String title = articleObj.getJSONObject("_source").getString("title");
                String avator = articleObj.getJSONObject("_source").getJSONObject("news_news__thumbnail_image").getString("thumbnail_image");
                String url = UrlUtils.buildUrl(source.getSpiderUrl(), articleObj.getJSONObject("_source").getString("url"));
                String summary = articleObj.getJSONObject("_source").getString("news_news__summary");
                String content = articleObj.getJSONObject("_source").getString("news_news__body");
                String published = articleObj.getJSONObject("_source").getString("publication_date_filter");
                BizArticle bizArticle = new BizArticle();
                bizArticle.setTitleOrg(title);
                bizArticle.setSummaryOrg(summary);
                bizArticle.setUrl(url);
                bizArticle.setAvator(avator);
                bizArticle.setPublishTime(published);
                bizArticle.setPublishFormatTime(DateUtils.formatDate(DateUtils.parseDate(published),published));
                bizArticle.setContentsOrg(content);
                bizArticle.setContentsCn(StringUtils.delHtmlTags(content));
                bizArticle.setCreateTime(DateUtils.getNowDate());
                bizArticle.setDataKey(EncryptUtil.encrypt(url, "md5"));
                bizArticle.setSourceId(source.getSourceId());
                System.out.println(bizArticle);
                BizArticle bizArticleDb = articleService.selectBizArticleByDataKey(bizArticle.getDataKey());
                if (StringUtils.isNull(bizArticleDb)){
                    //自动翻译标题
                    if (StringUtils.isContainsChinese(bizArticle.getTitleOrg())){
                        bizArticle.setTitleCn(bizArticle.getTitleOrg());
                    } else {
                        String transWord = getTransResult(bizArticle.getTitleOrg(), "auto", "zh");
                        JSONObject titleObj = JSONObject.parseObject(transWord);
                        JSONArray titleArray = titleObj.getJSONArray("trans_result");
                        if (StringUtils.isNotNull(titleArray) && titleArray.size() > 0){
                            bizArticle.setTitleCn(titleArray.getJSONObject(0).getString("dst"));
                        }
                    }

                    //自动翻译摘要
                    if(transConfig.isAbstractTrans() && StringUtils.isNotEmpty(bizArticle.getSummaryOrg())){
                        String transWord = getTransResult(bizArticle.getSummaryOrg(), "auto", "zh");
                        JSONObject titleObj = JSONObject.parseObject(transWord);
                        JSONArray titleArray = titleObj.getJSONArray("trans_result");
                        if (StringUtils.isNotNull(titleArray) && titleArray.size() > 0){
                            bizArticle.setSummaryCn(titleArray.getJSONObject(0).getString("dst"));
                        }
                    }

                    articleService.insertBizArticle(bizArticle);
                } else {
                    bizArticle.setId(bizArticleDb.getId());
                    articleService.updateBizArticle(bizArticle);
                }
            }
            Thread.sleep(source.getIntervalNum()*1000);
        }
    }

    public String getTransResult(String query, String from, String to) {
        Map<String, String> params = buildParams(query, from, to);
        return HttpClientUtil.doGet(transConfig.getTransUrl(), params,false).getContent();
    }

    private Map<String, String> buildParams(String query, String from, String to) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("q", query);
        params.put("from", from);
        params.put("to", to);
        params.put("appid", transConfig.getAppid());
        // 随机数
        String salt = String.valueOf(System.currentTimeMillis());
        params.put("salt", salt);
        // 签名
        String src = transConfig.getAppid() + query + salt + transConfig.getSecurityKey();
        params.put("sign", MD5.md5(src));
        return params;
    }

    public int getSize(){
        return size;
    }
}
